package com.mdd.admin.service.impl;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IVipRecordService;
import com.mdd.admin.validate.VipRecordCreateValidate;
import com.mdd.admin.validate.VipRecordSearchValidate;
import com.mdd.admin.validate.VipRecordUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.VipRecordDetailVo;
import com.mdd.admin.vo.VipRecordListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.VipRecord;
import com.mdd.common.mapper.VipRecordMapper;
import com.mdd.common.util.TimeUtils;

/**
 * vip开通记录实现类
 * @author DEREK
 */
@Service
public class VipRecordServiceImpl implements IVipRecordService {
        
    @Resource
    VipRecordMapper vipRecordMapper;

    /**
     * vip开通记录列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<VipRecordListedVo>
     */
    @Override
    public PageResult<VipRecordListedVo> list(PageValidate pageValidate, VipRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<VipRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        vipRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:userId@user_id:int",
            "like:orderSn@order_sn:str",
            "=:payWay@pay_way:int",
            "=:relatedId@related_id:int",
            "=:orderAmount@order_amount:str",
            "=:actualAmount@actual_amount:str",
            "=:orderCreateTime@order_create_time:str",
            "=:orderUpdateTime@order_update_time:str",
            "=:tradeNo@trade_no:str",
            "=:orderStatus@order_status:int",
            "=:remark:str",
            "=:extend:str",
        });

        IPage<VipRecord> iPage = vipRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<VipRecordListedVo> list = new LinkedList<>();
        for(VipRecord item : iPage.getRecords()) {
            VipRecordListedVo vo = new VipRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setOrderCreateTime(TimeUtils.timestampToDate(item.getOrderCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * vip开通记录详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return VipRecord
     */
    @Override
    public VipRecordDetailVo detail(Integer id) {
        VipRecord model = vipRecordMapper.selectOne(
                new QueryWrapper<VipRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        VipRecordDetailVo vo = new VipRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * vip开通记录新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(VipRecordCreateValidate createValidate) {
        VipRecord model = new VipRecord();
        model.setUserId(createValidate.getUserId());
        model.setOrderSn(createValidate.getOrderSn());
        model.setPayWay(createValidate.getPayWay());
        model.setRelatedId(createValidate.getRelatedId());
        model.setOrderAmount(createValidate.getOrderAmount());
        model.setActualAmount(createValidate.getActualAmount());
        model.setOrderCreateTime(createValidate.getOrderCreateTime());
        model.setOrderUpdateTime(createValidate.getOrderUpdateTime());
        model.setTradeNo(createValidate.getTradeNo());
        model.setOrderStatus(createValidate.getOrderStatus());
        model.setRemark(createValidate.getRemark());
        model.setExtend(createValidate.getExtend());
        vipRecordMapper.insert(model);
    }

    /**
     * vip开通记录编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(VipRecordUpdateValidate updateValidate) {
        VipRecord model = vipRecordMapper.selectOne(
                new QueryWrapper<VipRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setOrderSn(updateValidate.getOrderSn());
        model.setPayWay(updateValidate.getPayWay());
        model.setRelatedId(updateValidate.getRelatedId());
        model.setOrderAmount(updateValidate.getOrderAmount());
        model.setActualAmount(updateValidate.getActualAmount());
        model.setOrderCreateTime(updateValidate.getOrderCreateTime());
        model.setOrderUpdateTime(updateValidate.getOrderUpdateTime());
        model.setTradeNo(updateValidate.getTradeNo());
        model.setOrderStatus(updateValidate.getOrderStatus());
        model.setRemark(updateValidate.getRemark());
        model.setExtend(updateValidate.getExtend());
        vipRecordMapper.updateById(model);
    }

    /**
     * vip开通记录删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        VipRecord model = vipRecordMapper.selectOne(
                new QueryWrapper<VipRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        vipRecordMapper.delete(new QueryWrapper<VipRecord>().eq("id", id));
    }

}
